<template>
  <div>

    <div class="title">
      我的收藏
    </div>

    <div class="tableData">

      <div class="allCheck">
        <el-checkbox v-model="isAllSelected" @change="handleAllSelect">全选</el-checkbox>
        <span class="delent" style="cursor: pointer;" @click="deleteAllFunc">删除选中</span>
      </div>


      <div>


        <div v-for="(goods, index) in goodsList" :key="index" class="goods-item">
          <el-checkbox v-model="goods.checked"></el-checkbox>
          <img :src="goods.p_thumb" alt="商品图片">
          <div>
            <p class="goodsName">{{ goods.title }}</p>

          </div>

          <div class="goodsPrice">
            <p>￥{{ goods.p_price }}</p>
          </div>

          <button class="gwcButton" @click="addToCart(goods)">加入购物车</button>
          <button class="colseSc" @click="cancelFavorite(goods)">取消收藏</button>
        </div>
        <div v-if="goodsList.length == 0" class="nodata">暂无数据</div>

      </div>



    </div>

  </div>
</template>

<script>
import { mapState } from 'vuex';
export default {
  name: "SupplierCollect",
  data() {
    return {
      goodsList: [
        
      ],
      selectedGoods: [],
      isAllSelected: false
    }
  },
  computed: {
    ...mapState([
      "userInfo",
    ]),
  },
  created() {
    this.getData();
  },
  methods: {
    getData() {
      this.$httpApi("/api/getMyCollect", {
        token: this.userInfo.token,
        page: 1,
        limit: 10000
      }, 'get').then(res => {
        this.goodsList = res.data.list;
      })
    },
    addToCart(goods) {
      console.log(`将商品 ${goods.name} 加入购物车`);
      // 这里添加将商品加入购物车的实际逻辑，比如发送请求到后端
    },
    cancelFavorite(goods) {
      console.log(`取消收藏商品 ${goods.name}`);
      this.$confirm('确定取消收藏商品么', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      }).then(({ value }) => {
        this.$httpApi("/api/qxCollect", {
          token: this.userInfo.token,
          ids: goods.id
        }, "get").then(res => {
          if (res.code == 200) {
            console.log(res)
            this.getData()
            alertSucc(res.msg);
          } else {
            alertErr(res.msg);
          }
        })
      }).catch(() => {
      });
    },
    handleAllSelect() {
      if (this.isAllSelected) {
        this.goodsList.map(item => item.checked = true);
      } else {
        this.goodsList.map(item => item.checked = false);
      }
    },
    deleteAllFunc() {
      let temp = [];
      this.goodsList.map(item => {
        if (item.checked) {
          temp.push(item.id)
        }
      });
      if (temp.length == 0) {
        alertErr("请选择要删除的商品")
        return;
      } else {
        this.$confirm('确定取消收藏选中商品么', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
        }).then(({ value }) => {
          this.$httpApi("/api/qxCollect", {
            token: this.userInfo.token,
            ids: temp.join(",")
          }, "get").then(res => {
            if (res.code == 200) {
              console.log(res)
              this.getData()
              alertSucc(res.msg);
            } else {
              alertErr(res.msg);
            }
          })
        }).catch(() => {
        });
      }
    },
  }
}
</script>

<style lang="less" scoped>
.nodata{
  text-align: center;
  margin-top: 20px;
  font-size: 14px;
}
.goodsName {
  width: 275px;

  font-family: PingFang SC, PingFang SC;
  font-weight: 500;
  font-size: 14px;
  color: #333333;
  text-align: left;
  font-style: normal;
  text-transform: none;
}

.colseSc {

  font-family: Microsoft YaHei, Microsoft YaHei;
  font-weight: 400;
  font-size: 14px;
  color: #666666;

  font-style: normal;
  text-transform: none;
}

.gwcButton {
  width: 104px;
  height: 32px;
  background: #0052D9;
  border-radius: 4px 4px 4px 4px;
  border: 1px solid rgba(0, 0, 0, 0);

  font-family: Microsoft YaHei, Microsoft YaHei;
  font-weight: 400;
  font-size: 14px;
  color: #FFFFFF;
  margin-right: 10px;
  font-style: normal;
  text-transform: none;

}

.goodsPrice {
  margin-left: 115px;
  width: 310px;
  font-family: Arial, Arial;
  font-weight: 400;
  font-size: 14px;
  color: #000000;
  font-style: normal;
  text-transform: none;
}

/* 简单的样式 */
.goods-item {
  display: flex;
  align-items: center;
  padding: 10px;
  border-bottom: 1px solid #EBEEF5;
  padding-left: 24px;
  height: 120px;
}

.goods-item img {
  width: 110px;
  height: 80px;
  margin-right: 30px;
  margin-left: 70px;
}

.goods-item button {
  margin-left: 10px;
}


.delent {
  padding-left: 32px;
}

.tableData {
  padding: 20px 32px;
}

.allCheck {
  width: 1150px;
  height: 50px;
  background: #F5F5F5;
  border-radius: 0px 0px 0px 0px;
  padding: 0 24px;
  line-height: 50px;


  font-family: Microsoft YaHei, Microsoft YaHei;
  font-weight: 400;
  font-size: 14px;
  color: #666666;

  font-style: normal;
  text-transform: none;
}

.title {
  height: 56px;
  background: #FFFFFF;
  line-height: 56px;
  border-radius: 0px 0px 0px 0px;
  font-family: Microsoft YaHei, Microsoft YaHei;
  font-weight: bold;
  font-size: 16px;
  color: #333333;
  font-style: normal;
  text-transform: none;
  border-bottom: 4px solid #F5F5F5;
  padding-left: 32px;
}
</style>